import mqtt from "mqtt";


const mqttClient = (topic,params) => {
  return new Promise(resolve => {
    const client = mqtt.connect("ws://jis.joshvm.com:8083/mqtt", {
      keepalive: 5000,
      clientId: `QUERY${params.imei + Date.now()}`,
      protocolId: 'MQTT',
      protocolVersion: 4,
      clean: true,
      reconnectPeriod: 5000,
      connectTimeout: 60 * 1000,
      username:'bridge',
      password:'bridge123456',
      will: {
        topic: 'WillMsg',
        payload: 'Connection Closed abnormally..!',
        qos: 1,
        retain: true
      },
    })
    client.on("connect", function ( ) {
      client.subscribe(topic,function (err,granted) {
        if (!err) {
          console.log('订阅成功',granted);
        }
      })
    })
    // 订阅
    client.on("message", (topic, message) => {
      console.log(JSON.parse(message))
    })
    resolve(client)
  })
}

export default mqttClient
